package com.platinadesign.axa
{
	import com.platinadesign.axa.event.ConfigureEvent;
	import com.platinadesign.axa.event.ResourceEvent;
	import com.platinadesign.axa.loader.ConfigureLoader;
	import com.platinadesign.axa.loader.ResourceLoader;
	import com.platinadesign.axa.manager.EffectsManager;
	import com.platinadesign.axa.manager.LayoutManager;
	import com.platinadesign.axa.manager.MusicManager;
	import com.platinadesign.axa.manager.PagesManager;
	import com.platinadesign.axa.view.Preloader;
	import com.platinadesign.axa.view.SystemViewContainer;
	import com.platinadesign.axa.vo.ConfigueData;
	import com.platinadesign.axa.vo.Resource;
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;

	/**
	 * facade是习惯用语，在编程中，这个来自法语的词汇通常意味着总控制类，负责调配和插接各个类的关系，在这个工程中，
	 * 虽然没有使用流行框架，但是也沿用了这个习惯，这个类负责管理所有类的关系，以及挂接和初始化他们
	 * @author DuMing
	 *
	 */
	public class Facade
	{
		private var _stage : Stage;

		private var configLoader : ConfigureLoader;

		private var application : main;

		private var _preloader : Preloader;

		private var _layoutManager : LayoutManager;

		private var _resourceLoader : ResourceLoader;

		private var _effectManager : EffectsManager;

		private var _musicManager : MusicManager;

		private var _pagesManager : PagesManager;

		private var _messageBus : MessageBus;

		public function Facade( app : main )
		{
			application = app;
			_messageBus = MessageBus.getInstance();
			_messageBus.init( app.stage );
		}

		//------------------------ initializing---------------------------------
		public function initSystem( configURL : String ) : void
		{
			initResourceLoader();
			initConfig( configURL );
			_layoutManager = new LayoutManager( application );
			_effectManager = new EffectsManager();
			_musicManager = new MusicManager();
			_pagesManager = new PagesManager();
			//---init preloader
			_layoutManager.initPreloader();
		}

		//----------------------loading complete-------------------------
		protected function configureLoadedHandler( event : ConfigureEvent ) : void
		{
			const configure : ConfigueData = event.configData;
			_resourceLoader.loadResource( configure );
			_musicManager.initMusic( configure.musicAddress );
		}

		protected function allLoadedHandler( event : ResourceEvent ) : void
		{
			trace( event.type );
			_layoutManager.initView( event.resource );
			_pagesManager.initPages( event.resource.pages );
		}

		protected function resourceLoadedHandler( event : ResourceEvent ) : void
		{
			_layoutManager.setProgress( event.loadingVO )
		}

		//---------------------------error---------------------------
		protected function resourceLoadErrorHandler( event : ResourceEvent ) : void
		{
			trace( event.type + "  " + event.message );
		}

		protected function configureLoadErrorHandler( event : ConfigureEvent ) : void
		{
			trace( event.type )
		}

		//---------------------------sub init--------------------------------------
		private function initConfig( configURL : String ) : void
		{
			configLoader = new ConfigureLoader();
			configLoader.addEventListener( ConfigureEvent.CONFIGURE_LOADED, configureLoadedHandler );
			configLoader.addEventListener( ConfigureEvent.CONFIGURE_LOAD_ERROR, configureLoadErrorHandler );
			configLoader.loadConfigure( configURL );
		}

		private function initResourceLoader() : void
		{
			_resourceLoader = new ResourceLoader();
			_resourceLoader.addEventListener( ResourceEvent.ALL_RESOURCE_LOADED, allLoadedHandler );
			_resourceLoader.addEventListener( ResourceEvent.RESOURCE_LOADED, resourceLoadedHandler );
			_resourceLoader.addEventListener( ResourceEvent.RESOURCE_LOAD_ERROR, resourceLoadErrorHandler );
		}
	}
}